package makovo.controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

import makovo.data.Resources;
import makovo.entities.ApplicantWithDocs;
import makovo.entities.Document;
import makovo.utils.FTPAccess;

public class deleteApplicant extends ActionSupport implements SessionAware{
	/**
	 * 
	 */
	private String curp;
	private Map<String, Object> sessionmap;
	
	private static final long serialVersionUID = 1L;
	
	public void setCurp(String curp)
	{
		this.curp = curp;
	}
	
	public String getCurp()
	{
		return this.curp;
	}
	public String execute() {
		String accion = "ERROR";
		
		try {
			ApplicantWithDocs app = Resources.getApplicantWithDocs_ftp(curp);
			ArrayList<Document> docs = app.getDocs();
			for(int i=0; i < docs.size(); i++)
			{
				String file= docs.get(i).getPath();
				FTPAccess.getInstance().removeFile(file);
			}//AQUI ME QUEDE
			String directory = curp;
			FTPAccess.getInstance().deleteDirectory(directory);
			String email = sessionmap.get("login.email").toString();
			if(Resources.deleteApplicant(curp, email)){  
				accion = "SUCCESS";  
			}  
			else{  
				accion = "ERROR";  
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}  catch (Exception e){
			System.out.println(e.getMessage());
		}
		
		return accion;
	}

	public boolean borrar()
	{
		return true;
	}
	@Override
	public void setSession(Map<String, Object> map) {
		// TODO Auto-generated method stub
		 this.sessionmap = map; 
		
	}
}
